package org.graduation.utils;


import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Component
public class Encrypt {

    private static final String SALT = "sadnuia1232miawlk";

    public Encrypt() {
    }

    public String md5_32(String str) {
        try {
            // 获取MD5算法的MessageDigest实例
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 用输入的字节更新摘要
            md.update(str.getBytes());
            md.update(SALT.getBytes());
            // 完成哈希计算并返回结果
            byte[] digest = md.digest();
            // 将得到的字节数组转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            // 如果MD5算法不可用，则抛出异常
            throw new RuntimeException("MD5 algorithm is not available", e);
        }
    }
}
